using System;
using System.Collections.Generic;
using System.Text;

namespace TaskVision
{
    class ValidateIsraelID
    {
        public static int CheckLegalID(string checkID)
        {
            int lID, lCnt, lSum, lCheckDigit;
            string sID;
            int retval = -1;
            lID = Convert.ToInt32(checkID);
            sID = lID.ToString();
            sID = sID.PadLeft(9, '0');
            lSum = 0;
            for (lCnt = 1; lCnt <= 8; lCnt++)
            {
                if ((lCnt % 2) == 0)
                {
                    switch (Convert.ToInt32(sID.Substring(lCnt - 1, 1)))
                    {
                        case 0:
                        case 1:
                        case 2:
                        case 3:
                        case 4: { lSum = lSum + Convert.ToInt32(sID.Substring(lCnt - 1, 1)) * 2; break; }
                        case 5: { lSum++; break; }
                        case 6: { lSum += 3; break; }
                        case 7: { lSum += 5; break; }
                        case 8: { lSum += 7; break; }
                        case 9: { lSum += 9; break; }
                    }
                }
                else { lSum = lSum + Convert.ToInt32(sID.Substring(lCnt - 1, 1)); }
            }

            lCheckDigit = (10 - lSum % 10) % 10;
            if (lCheckDigit == Convert.ToInt32(sID.Substring(8, 1)))
                retval = -1;
            else
                retval = lCheckDigit;

            return retval;
        }

        public static bool IsCorrectIDnumber(string IsraelID)
        {
            //Check length 8 or 9 digit
            if ((IsraelID.Trim().Length != 8) && (IsraelID.Trim().Length != 9))
                return false;

            //remove leading zeros
            string IDnumber = Convert.ToInt32(IsraelID).ToString();

            if (ValidateIsraelID.CheckLegalID(IDnumber) != -1)
                return false;
            
            return true;
        }
    }
}
